/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：太平金科-基础组件封装层-ElasticSearch-缺省实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taipingframework.boot.es.facade;

import com.taipingframework.utility.exception.UtilityException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.settings.Settings;

import java.util.Map;

/**
 * 索引库管理
 */
public final class IndexAdmin {

    private static RestHighLevelClient restHighLevelClient;

    public IndexAdmin(RestHighLevelClient restHighLevelClient) {
        IndexAdmin.restHighLevelClient = restHighLevelClient;
    }

    /**
     * 索引是否存在
     */
    public static boolean existsIndex(String indexName) throws UtilityException {
        try {
            GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
            return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new UtilityException(e);
        }
    }

    /**
     * 创建索引
     */
    public static boolean createIndex(String indexName, int shards, int replicas) throws UtilityException {
        try {
            Settings settings = Settings.builder()
                    // 分片
                    .put("number_of_shards", shards)
                    // 副本，单个节点的ES集群无法分配副本节点
                    .put("number_of_replicas", replicas)
                    .build();
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(settings);
            CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

            return indexName.equalsIgnoreCase(createIndexResponse.index());
        } catch (Exception e) {
            throw new UtilityException(e);
        }
    }

    /**
     * 获取索引
     */
    public static Map<String, Settings> getIndex(String indexName) throws UtilityException {
        try {
            GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
            GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
            return getIndexResponse.getSettings();
        } catch (Exception e) {
            throw new UtilityException(e);
        }
    }

    /**
     * 删除索引
     */
    public static boolean deleteIndex(String indexName) throws UtilityException {
        try {
            DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indexName);
            AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteRequest, RequestOptions.DEFAULT);
            return acknowledgedResponse.isAcknowledged();
        } catch (Exception e) {
            throw new UtilityException(e);
        }
    }

}
